import { createWebHistory, createRouter } from 'vue-router'
import LoginView from '/src/view/login/LoginView.vue'

const routes = [
    {
        path: '/',
        name: 'layout',
        component: ()=>import('/src/view/layout/LayoutView.vue'),
        redirect: '/home',
        children: [
            {
                path: '/home',
                name: 'home',
                component: ()=> import('/src/view/index/IndexView.vue')
            },
            {
                path: '/user',
                name: 'user',
                component: ()=> import('/src/view/user/UserView.vue')
            },
            {
                path:'/student',
                name: 'student',
                component: ()=>import('/src/view/student/StudentView.vue')
            },
            {
                path:'/course',
                name: 'course',
                component: ()=>import('/src/view/course/CourseView.vue')
            },
            {
                path:'/grade',
                name: 'grade',
                component: ()=>import('/src/view/grade/GradeView.vue')
            },
            {
                path: '/info',
                name: '个人中心',
                component: ()=>import('/src/view/user/UserInfoView.vue')
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: LoginView
    },
]


const router = createRouter({
    history: createWebHistory(),
    routes,
})
router.beforeEach((to, from, next) => {
    const IsLogin = localStorage.getItem('user')
    if(!IsLogin && to.path !== '/login'){
        next('/login')
        console.log(IsLogin)
        ElMessage({
            message: '请先登录',
            type: 'warning',
        })
    }else {
        // if(to.path === '/login'){
        //     if(IsLogin){
        //         console.log(IsLogin)
        //         next('/')
        //         ElMessage({
        //             message: '已登录',
        //             type: 'warning',
        //         })
        //     }else next()
        // }
        next()
    }
})
export default router
